#include "data_utils.h"
#ifndef ASCENDC_CPU_DEBUG
#include "acl/acl.h"
extern void nms_with_mask_custom_do(uint32_t blockDim, void *stream, uint8_t *x,uint8_t *y, uint8_t *z, float iou_threshold, float scores_threshold);
#else
#include "tikicpulib.h"
extern "C" __global__ __aicore__ void nms_with_mask_custom(GM_ADDR x,GM_ADDR y, GM_ADDR z, float iou_threshold, float scores_threshold);
#endif

int32_t main(int32_t argc, char *argv[])
{
    uint32_t blockDim = 1;
    size_t inputByteSize = 20 * 4 * sizeof(float);
    size_t inputByteSize1 = 20  * sizeof(float);
    size_t outputByteSize = 20 * sizeof(uint8_t);
    
    float iou_threshold = 0.1f; 
    float scores_threshold = 0.1f;  // 示例值，你可以根据需要修改

#ifdef ASCENDC_CPU_DEBUG
    uint8_t *x = (uint8_t *)AscendC::GmAlloc(inputByteSize);
    uint8_t *y = (uint8_t *)AscendC::GmAlloc(inputByteSize1);
    uint8_t *z = (uint8_t *)AscendC::GmAlloc(outputByteSize);

    ReadFile("./input/input_x.bin", inputByteSize, x, inputByteSize);
    ReadFile("./input/input_y.bin", inputByteSize1, y, inputByteSize1);

    AscendC::SetKernelMode(KernelMode::AIV_MODE);

    ICPU_RUN_KF(nms_with_mask_custom, blockDim, x, y, z, iou_threshold, scores_threshold); 

    WriteFile("./output/output_z.bin", z, outputByteSize);
    

    AscendC::GmFree((void *)x);
    AscendC::GmFree((void *)y);
    AscendC::GmFree((void *)z);
#else
    CHECK_ACL(aclInit(nullptr));
    int32_t deviceId = 0;
    CHECK_ACL(aclrtSetDevice(deviceId));
    aclrtStream stream = nullptr;
    CHECK_ACL(aclrtCreateStream(&stream));

    uint8_t *xHost,*yHost, *zHost;
    uint8_t *xDevice,*yDevice, *zDevice;

    CHECK_ACL(aclrtMallocHost((void **)(&xHost), inputByteSize));
    CHECK_ACL(aclrtMallocHost((void **)(&yHost), inputByteSize1));
    CHECK_ACL(aclrtMallocHost((void **)(&zHost), outputByteSize));
    CHECK_ACL(aclrtMalloc((void **)&xDevice, inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&yDevice, inputByteSize1, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&zDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));

    ReadFile("./input/input_x.bin", inputByteSize, xHost, inputByteSize);
    ReadFile("./input/input_y.bin", inputByteSize1, yHost, inputByteSize1);

    CHECK_ACL(aclrtMemcpy(xDevice, inputByteSize, xHost, inputByteSize, ACL_MEMCPY_HOST_TO_DEVICE));
    CHECK_ACL(aclrtMemcpy(yDevice, inputByteSize1, yHost, inputByteSize1, ACL_MEMCPY_HOST_TO_DEVICE));

    
    nms_with_mask_custom_do(blockDim, stream, xDevice,yDevice, zDevice, iou_threshold, scores_threshold);
    CHECK_ACL(aclrtSynchronizeStream(stream));

    CHECK_ACL(aclrtMemcpy(zHost, outputByteSize, zDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST));
    WriteFile("./output/output_z.bin", zHost, outputByteSize);

    CHECK_ACL(aclrtFree(xDevice));
    CHECK_ACL(aclrtFree(yDevice));
    CHECK_ACL(aclrtFree(zDevice));
    CHECK_ACL(aclrtFreeHost(xHost));
    CHECK_ACL(aclrtFreeHost(yHost));
    CHECK_ACL(aclrtFreeHost(zHost));

    CHECK_ACL(aclrtDestroyStream(stream));
    CHECK_ACL(aclrtResetDevice(deviceId));
    CHECK_ACL(aclFinalize());
#endif
    return 0;
}